const fs = require('fs')
const { AVATAR_PATH } = require('../constants/file-path')
const userService = require('../service/user.service')
class UserController {
  async create(ctx, next) {
    // 向数据库中插入数据
    const result = await userService.create(ctx.request.body)
    // 给用户返回数据
    ctx.response.body = result
  }
  async avatarInfo(ctx, next) {
    const { userId } = ctx.params
    const avatarInfo = await userService.getAvatarInfoByuserId(userId)
    ctx.response.set('content-type', avatarInfo.mimetype)
    ctx.body = fs.createReadStream(`${AVATAR_PATH}/${avatarInfo.filename}`)
  }
}

module.exports = new UserController()
